<?php

defined('SYSPATH') or die('No direct script access.');

class Controller_Helper_Generic {

    public static function add_session_upload($url_media, $id, $type, $session) {
        $config = Kohana::$config->load('config')->as_array() + array(
            'php_exec' => '',
            'php_index' => '',
        );
        // Extract configured options
        extract($config, EXTR_SKIP);

        if (substr(php_uname(), 0, 7) == "Windows") {
            $cmd = "$php_exec $php_index --task=Addsessionupload --url_media=$url_media --id=$id --type=$type --session=$session";
            pclose(popen("start /B " . $cmd, "r"));
        }
        //for linux
        else {
            $cmd = "$php_exec $php_index --task=Addsessionupload --url_media=$url_media --id=$id --type=$type --session=$session";
            exec($cmd . " > /dev/null &");
        }
    }

    public static function create_customer_code($type = null) {
        $code = '';
        if ($type == Controller_Helper_Constant::STUDENT_SALE) {
            while (1) {
                $code = self::get_random_string('ABCDEFGHIJKLMNOPQRSTUVWXYZ', 5);
                $code = $code . self::get_random_string('0123456789', 5);
                $s_customer = new Model_Service_Customer();
                $n = $s_customer->load_all(true, null, null, array('customer_code'=>$code));
                if($n == 0)break;
            }
        }
        return $code;
    }

    public static function get_random_string($origin, $length) {
        $str_len = strlen($origin);
        $random_string = '';
        for ($j = 0; $j < $length; $j++) {
            $rand = mt_rand(0, $str_len - 1);
            $random_string = $random_string . $origin[$rand];
        }
        return $random_string;
    }

}

?>
